package com.foryou.system.common.utils;


import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

public class CommUtil {

	private final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999,
			Integer.MAX_VALUE };
	
	/**
	 * 
	* @Title: sizeOfInt
	* @Description: (判断整数多少位)
	* @param @param x
	* @param @return    参数
	* @return int    int 位数
	* @throws
	 */
	public static int sizeOfInt(int x) {
		for (int i = 0;; i++)
			if (x <= sizeTable[i])
				return i + 1;
	}

	/**
	 * 
	* @Title: currentTime
	* @Description: (获取当前时间)
	* @param @return    参数
	* @return String    当前时间
	* @throws
	 */
	public static String getCurrentTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
	

	/**
	 * 顺番自增补零操作
	 * @param code 需要补零的字符串
	 * @return 补零后的字符串
	*/
	public static String zeroize(String code){  
		Integer intCode = Integer.parseInt(code);
		intCode++;  
		DecimalFormat df = new DecimalFormat("0000");
		return df.format(intCode);  
	}
	
	/**
	 * bean转List
	 * 
	 * @param obj
	 *            需要补零的字符串
	 * @return 转换后的list
	 * @throws Exception 
	 */
	public static List<Map<String, String>> transBeanToList(Object obj) throws Exception{

		// 如果对象为null
		if (obj == null) {
			return null;
		}
		// 初始化返回list
		List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();

		Map<String, String> map = null;
		// 转换为bean
		BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
		// 取得bean元素
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		// 循环元素
		for (PropertyDescriptor property : propertyDescriptors) {
			map = new HashMap<String, String>();
			String key = property.getName();
			// 过滤class属性
			if (!key.equals("class")) {
				// 得到property对应的getter方法
				Method getter = property.getReadMethod();
				Object value = getter.invoke(obj);
				if (null != value) {
					map.put("key", key);
					map.put("value", value.toString());
					resultList.add(map);
				}
			}
		}
		return resultList;
	}

	/**
	 * distinctByKey:自定义拉姆达表达式去重
	 *
	 * @param keyExtractor
	 * @return Predicate<T>
	 */
	public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
		Map<Object, Boolean> seen = new ConcurrentHashMap<>();
		return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
	}

	/**
	 * 比较两个list中对象属性是否相等
	 * @param a
	 * @param b
	 * @param <T>
	 * @return
	 */
	public static <T> boolean equals(List<T> a, List<T> b) {

		if (a.size() != b.size())
			return false;

		if (a.isEmpty())
			return false;

		if (b.isEmpty())
			return false;

		T q;
		for (int i = 0; i < a.size(); i++) {
			q = a.get(i);
			for (int x = 0; x < b.size(); x++) {
				if (q.equals(b.get(x))) {
					System.out.println("有一个元素的值是相等的");
					break;
				} else {
					if (x == b.size()) {
						System.out.println("没有一个元素是相等的");
						return false;
					}
				}
			}
		}
		return true;
	}
}
